package com.teamit.web;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teamit.entity.Employee;
import com.teamit.entity.Project;
import com.teamit.service.EmployeeProjectService;
import com.teamit.service.EmployeeService;
import com.teamit.service.ProjectService;

public class NewProject extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		EmployeeService es=new EmployeeService();
		List<Employee> employeeAll=es.findAll();
		request.setAttribute("AllEmployee", employeeAll);
		request.getRequestDispatcher("/WEB-INF/views/new_project.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String name=request.getParameter("project");
		String desc=request.getParameter("projectDesc");
		String employee=request.getParameter("employee");
		String member=request.getParameter("member");
		StringBuffer sb=new StringBuffer(employee+",");
		sb.append(member);
		String[] strs=sb.toString().split(",");
		Project project=new Project();
		UUID uuid=UUID.randomUUID();
		project.setId(uuid.toString());
		project.setName(name);
		project.setDesc(desc);
		ProjectService ps=new ProjectService();
		ps.save(project);
		EmployeeService es=new EmployeeService();
		EmployeeProjectService eps=new EmployeeProjectService();
		int role=0;
		for(String str:strs){
			System.out.println(str);
			String id=es.findIdByName(str);
			if(id!=null&&!id.equals("")){
				eps.save(id, uuid.toString(), role);
				role++;
			}
		}
		response.sendRedirect("main.jspx");
	}

}
